﻿using System.Timers;

namespace Builderator.Core
{
    public class IntervalTimer
    {
        public delegate void Trigger();

        public event Trigger TriggerEvent;

        private System.Timers.Timer timer;

        public IntervalTimer(double interval, Trigger trigger)
        {
            this.timer = new System.Timers.Timer(interval);

            this.TriggerEvent = trigger;

            this.timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
        }

        private void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            this.TriggerEvent();
        }

        public void Start()
        {
            this.timer.Enabled = true;
        }

        public void Stop()
        {
            this.timer.Enabled = false;
        }

        public void Restart()
        {
            this.timer.Enabled = false;

            this.timer.Enabled = true;
        }
    }
}
